﻿using System;
using System.IO;
using System.Text;
using System.Collections.Generic;
using System.Drawing;

namespace Projky.Common {

    class ScriptIOHelper {

        internal static ScriptFolder[] GetFolders() {
            List<string> list = new List<string>();

            string projkyFolder = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\ProjkyAddin";
            if (Directory.Exists(projkyFolder) == false) {
                Directory.CreateDirectory(projkyFolder);
            }

            list.Add(projkyFolder);

            string configFilePath = Path.Combine(Path.GetDirectoryName(typeof(ScriptIOHelper).Assembly.Location), "scriptfolders.txt");

            foreach (var folder in ReadConfigFile(configFilePath)) {
                if (Directory.Exists(folder) == false) {
                    continue;
                }
                list.Add(folder);
            }

            ScriptFolder[] scriptFolders = new ScriptFolder[list.Count];

            scriptFolders[0] = new ScriptFolder(projkyFolder, "ProjkyAddin");

            for (int i = 1; i < list.Count; i++) {
                string barName = GenerateBarName(list[i]);
                scriptFolders[i] = new ScriptFolder(list[i], barName);
            }

            return scriptFolders;
        }

        static string GenerateBarName(string directory) {
            return directory.Replace("\\", "_").Replace(":","_").Replace(" ","_");
        }

        internal static string[] ReadConfigFile(string configFilePath) {
            if (File.Exists(configFilePath) == false) { 
                return new string[0];
            }

            if (IsUTF8Files(File.ReadAllBytes(configFilePath)) == true) {
                return File.ReadAllLines(configFilePath, Encoding.UTF8);
            } else {
                return File.ReadAllLines(configFilePath, Encoding.Default);
            }
        }

        internal static string ReadFile(string filePath) {
            if (File.Exists(filePath) == false) {
                return filePath + " file not exists.";
            }

            if (IsUTF8Files(File.ReadAllBytes(filePath)) == true) {
                return File.ReadAllText(filePath, Encoding.UTF8);
            } else {
                return File.ReadAllText(filePath, Encoding.Default);
            }
        }

        internal static Bitmap GetScriptBitmap() {
            if (_scriptImage != null) {
                return _scriptImage;
            }

            string imagePath = Path.Combine(Path.GetDirectoryName(typeof(ScriptIOHelper).Assembly.Location), "Script.png");
            if (File.Exists(imagePath) == false) {
                return null;
            }

            _scriptImage = new Bitmap(Image.FromFile(imagePath));

            return _scriptImage;
        }

        // is utf8 encoding file
        private static bool IsUTF8Files(byte[] data) {
            int byteCount = 1;
            byte tByte;
            for (int i = 0; i < data.Length; i++) {
                tByte = data[i];
                if (byteCount == 1) {
                    if (tByte >= 0x80) {
                        while (((tByte <<= 1) & 0x80) != 0) {
                            byteCount++;
                        }
                        if (byteCount == 1 || byteCount > 6) {
                            return false;
                        }
                    }
                } else {
                    if ((tByte & 0xC0) != 0x80) {
                        return false;
                    }
                    byteCount--;
                }
            }
            if (byteCount > 1) {
                return false;
            }
            return true;
        }

        static Bitmap _scriptImage;
    }
}
